package com.lovely602.ai.openai.controller.chat;


import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping("/chat/client")
public class ChatClientController {


    private final ChatClient chatClient;

    public ChatClientController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    /**
     * 聊天
     * @param msg 用户输入信息
     * @return 模型返回信息
     */
    @GetMapping("/chat")
    public String chat(@RequestParam(value = "msg", required = true) String msg) {

        String content = "";
        try {
            content = chatClient.prompt() // 提示词
                    .user(msg) // 用户输入信息
                    .call() // 访问大模型
                    .content();// 返回文本
        } catch (Exception e) {
            log.error("chat error: ", e);
        }
        return content;
    }
}
